//
//  CPHomeVC.m
//  CPApp
//
//  Created by 健健 on 2018/3/21.
//  Copyright © 2018年 Class. All rights reserved.
//




#import "CPHomeVC.h"
#import "CPHomeHeader.h"
#import "CPHomeCell.h"
#import "CPHomeModel.h"
#import "CPRightview.h"
#import "TZPreviewController.h"

@interface CPHomeVC ()
@property(nonatomic,strong) CPRightview * rightView;
@property(nonatomic,assign) BOOL needRight;
@property(nonatomic,strong) UIView *back;
@property(nonatomic,strong) CPHomeHeader * headerView;
@property(nonatomic,strong) NSString * playtype;
@property(nonatomic,strong) NSString * lottype;
@property(nonatomic,assign) BOOL isRightHide;
@end

@implementation CPHomeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"福彩3D";
    [self configHeader];
    self.playtype = @"1038";
    self.lottype = @"1002";
    self.rightNavImageName = @"HomeRight";
    [self loadNetworkData];
    [mNotificationCenter addObserver:self selector:@selector(headerBtnNotification:) name:@"headerBtnNotification" object:nil];
    [mNotificationCenter addObserver:self selector:@selector(gameShowBtnNotification) name:@"gameShowBtnNotification" object:nil];
    [mNotificationCenter addObserver:self selector:@selector(categoryBtnNotification:) name:@"categaryBtnNotification" object:nil];
}
-(void)configHeader {
    self.headerView = [[CPHomeHeader alloc]init];
    self.headerView.titles = [@[@"杀二码",@"独胆",@"双胆",@"三胆",@"个位杀二码",@"十位杀二码",@"百位杀二码"] mutableCopy];
    self.headerView.showSpots = @[@1,@1,@1,@1,@1,@1,@1].mutableCopy;
    self.headerView.navTitle = self.title;
    [self.view addSubview:self.headerView];
}

-(void)headerBtnNotification:(NSNotification *)not {
    NSString * index = not.userInfo[@"index"];
    self.playtype = index;
    self.lottype = not.userInfo[@"lottype"];
    [self loadNetworkData];
}
#pragma mark -- 分类列表视图
-(void)categoryBtnNotification:(NSNotification *)not {
    NSString * tag = not.userInfo[@"sender"];
    self.needRight = NO;
    self.rightView.hidden = YES;
    self.back.hidden = YES;
    switch (tag.integerValue) {
        case 100:
            self.title = @"双色球";
            self.headerView.titles = [@[@"杀三码",@"杀六码",@"杀十码",@"蓝球杀五码",@"蓝球定三胆",@"蓝球定五胆",@"蓝球杀号"] mutableCopy];
            self.headerView.showSpots = @[@1,@1,@1,@1,@1,@1,@1].mutableCopy;
            self.playtype = @"1039";
            self.lottype = @"1001";
            break;
        case 101:
            self.title = @"福彩3D";
            self.headerView.titles = [@[@"杀二码",@"独胆",@"双胆",@"三胆",@"个位杀二码",@"十位杀二码",@"百位杀二码"] mutableCopy];
            self.headerView.showSpots = @[@1,@1,@1,@1,@1,@1,@1].mutableCopy;
            self.playtype = @"1038";
            self.lottype = @"1002";
            break;
        case 102:
            self.title = @"七乐彩";
            self.headerView.titles = @[@"杀三码",@"杀六码",@"杀十码",@"独胆",@"双胆"].mutableCopy;
            self.headerView.showSpots = @[@1,@1,@1,@1,@1].mutableCopy;
            self.playtype = @"1039";
            self.lottype = @"1003";
            break;
        case 103:
            self.title = @"七星彩";
            self.headerView.titles = @[@"第一位杀二码",@"第二位杀二码",@"第三位杀二码",@"第四位杀二码",@"第五位杀二码",@"第六位杀二码",@"第七位杀二码"].mutableCopy;
            self.headerView.showSpots = @[@1,@1,@1,@1,@1,@1,@1].mutableCopy;
            self.playtype = @"1055";
            self.lottype = @"1004";
            break;
        case 104:
            self.title = @"排列三";
            self.headerView.titles = @[@"杀二码",@"独胆",@"双胆",@"三胆",@"个位杀二码",@"十位杀二码",@"百位杀二码"].mutableCopy;
            self.headerView.showSpots = @[@1,@1,@1,@1,@1,@1,@1].mutableCopy;
            self.playtype = @"1038";
            self.lottype = @"1005";
            break;
        case 105:
            self.title = @"排列五";
            self.headerView.titles = @[@"个位杀二码",@"十位杀二码",@"百位杀二码",@"千位杀二码",@"万位杀二码"].mutableCopy;
            self.headerView.showSpots = @[@1,@1,@1,@1,@1].mutableCopy;
            self.playtype = @"1050";
            self.lottype = @"1006";
            break;
        case 106:
            self.title = @"大乐透";
            self.headerView.titles = @[@"杀三码",@"杀六码",@"杀十码",@"独胆",@"双胆",@"后区杀二码",@"后去区定六码"].mutableCopy;
            self.headerView.showSpots = @[@1,@1,@1,@1,@1,@1,@1].mutableCopy;
            self.playtype = @"1039";
            self.lottype = @"1007";
            break;
        default:
            break;
    }
    self.headerView.navTitle = self.title;
    [self loadNetworkData];
}

-(void)loadNetworkData {
    [ProgressHUD showLoading];
    [HttpTool getApi:@"http://139.196.187.18:19793/cpyc/last" params:@{@"lottype":self.lottype,@"playtype":self.playtype} success:^(id responseData) {
        [ProgressHUD dismiss];
        self.headerView.lotteryNoLabel.text = [NSString stringWithFormat:@"开奖号码 %@期",responseData[@"kjissue"]];
        NSString * cpNum = [responseData[@"kjnum"] stringByReplacingOccurrencesOfString:@"#" withString:@","];
        NSArray * arrangement = [cpNum componentsSeparatedByString:@","];
        self.headerView.arrangementArr = arrangement;
        NSArray * array = [CPHomeModel mj_objectArrayWithKeyValuesArray:responseData[@"list"]];
        [self.models removeAllObjects];
        [self.models addObjectsFromArray:array];
        [self.tableView endRefresh];
        [self.tableView reloadData];
    } failure:^(NSString *errorMsg) {
        [ProgressHUD show:errorMsg];
    }];
}
-(void)gameShowBtnNotification {
    TZPreviewController * preViewVC = [[TZPreviewController alloc]init];
    preViewVC.filename = @"介绍玩法.rtf";
    preViewVC.title = @"玩法说明";
    preViewVC.dataSource = self;
    CPNavigationController * navVC = [[CPNavigationController alloc]initWithRootViewController:preViewVC];
    [self presentViewController:navVC animated:YES completion:nil];
//    [self.navigationController pushViewController:preViewVC animated:YES];;
}
-(void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.headerView.frame = CGRectMake(0, 0, mScreen_W, 101);
    self.tableView.frame = CGRectMake(0, CGRectGetMaxY(self.headerView.frame), mScreen_W, mScreen_H - CGRectGetMaxY(self.headerView.frame)-TabBar_H-Nav_H);
}

-(void)configTableView {
    self.needHeaderRefresh= YES;
    self.tableViewStyle = UITableViewStylePlain;
    [super configTableView];
    [self.tableView registerCellByNibName:@"CPHomeCell"];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.models.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CPHomeCell * cell = [tableView dequeueReusableCellWithIdentifier:@"CPHomeCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    CPHomeModel * model = self.models[indexPath.row];
    cell.model = model;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

-(void)didClickRightNavAction {
    self.isRightHide = !self.isRightHide;
    if (self.isRightHide) {
        self.needRight = YES;
        self.rightView.hidden = NO;
        self.back.hidden = NO;
    }else {
        self.needRight = NO;
        self.rightView.hidden = YES;
        self.back.hidden = YES;
    }
    
}
-(void)setNeedRight:(BOOL)needRight {
    _needRight = needRight;
    if (needRight && !self.rightView && !self.back) {
        self.rightView = [[CPRightview alloc]init];
        self.rightView.frame = CGRectMake(0, 0, mScreen_W, 145);
        self.rightView.hidden = YES;
        [self.view addSubview:self.rightView];
        [self.view bringSubviewToFront:self.rightView];
        
        self.back = [UIView new];
        self.back.backgroundColor = [UIColor blackColor];
        self.back.alpha = 0.3;
        self.back.hidden = YES;
        self.back.frame = CGRectMake(0, CGRectGetMaxY(self.rightView.frame), mScreen_W, mScreen_H);
        [self.view addSubview:self.back];
        [self.view bringSubviewToFront:self.back];
    }
}
@end
